/*
Copyright To change this template, choose Tools | Templates
Copyright and open the template in the editor.
 */
package com.click.app.model;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * @author Joel Chuca Condo
 * @date 07/04/2009
 */
@Entity
@Table(name = "purchase_details")
public class PurchaseDetail extends BaseObject {

    private Long id;
    private Purchase purchase;
    private Material material;
    //private Coin coin;
    private Double quantity;
    private Double unitPrice;
    private Double currentQuantity;
    private Double totalPrice;
    private Double discountXfail;
    private Double quantityFail;
    
    private List<PackageDetail> packageDetails = new ArrayList<PackageDetail>();

    private List<PurchaseDetailCutProcess> listPurchaseDetailCutProcess
                                    = new ArrayList<PurchaseDetailCutProcess>();
    private List<ProcessDressmakerPurchaseDetail> listProcessDressmakerPurchaseDetail
                                    = new ArrayList<ProcessDressmakerPurchaseDetail>();
    private Boolean deleted;
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id_purchase_detail", unique = true, nullable = false)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "id_purchase", nullable = false)
    public Purchase getPurchase() {
        return this.purchase;
    }

    public void setPurchase(Purchase purchase) {
        this.purchase = purchase;
    }

    @ManyToOne(optional=true, fetch = FetchType.LAZY)
    @JoinColumn(name = "id_material")
    public Material getMaterial() {
        return this.material;
    }

    public void setMaterial(Material material) {
        this.material = material;
    }

    @Column(name = "discount_x_fail")
    public Double getDiscountXfail() {
        return discountXfail;
    }

    public void setDiscountXfail(Double discountXfail) {
        this.discountXfail = discountXfail;
    }

    @Column(name = "quantity_fail")
    public Double getQuantityFail() {
        return quantityFail;
    }

    public void setQuantityFail(Double quantityFail) {
        this.quantityFail = quantityFail;
    }

/*
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_coin", nullable = false)
    public Coin getCoin() {
        return this.coin;
    }

    public void setCoin(Coin coins) {
        this.coin = coin;
    }
*/
    @Column(name = "quantity", precision = 22, scale = 0)
    public Double getQuantity() {
        return this.quantity;
    }

    public void setQuantity(Double quantity) {
        this.quantity = quantity;
    }

    @Column(name = "unit_price", precision = 12, scale = 0)
    public Double getUnitPrice() {
        return this.unitPrice;
    }

    public void setUnitPrice(Double unitPrice) {
        this.unitPrice = unitPrice;
    }

    @Column(name = "total_price", precision = 12, scale = 0)
    public Double getTotalPrice() {
        return this.totalPrice;
    }

    public void setTotalPrice(Double totalPrice) {
        this.totalPrice = totalPrice;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "purchaseDetail")
    public List<PackageDetail> getPackageDetails() {
        if (packageDetails==null){
            packageDetails= new ArrayList<PackageDetail>();
        }
        return packageDetails;
    }

    public void setPackageDetails(List<PackageDetail> packageDetails) {
        this.packageDetails = packageDetails;
    }
    
    @Column(name = "deleted")
    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    /*
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "purchaseDetails")
    public Set<MaterialReturns> getMaterialReturnses() {
        return this.materialReturnses;
    }

    public void setMaterialReturnses(Set<MaterialReturns> materialReturnses) {
        this.materialReturnses = materialReturnses;
    }*/

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PurchaseDetail)) {
            return false;
        }
        PurchaseDetail other = (PurchaseDetail) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.click.app.model.PurchaseDetail[id=" + id + ", quantity="+quantity+"]";
    }

    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "purchaseDetail")
    public List<PurchaseDetailCutProcess> getListPurchaseDetailCutProcess() {
        return listPurchaseDetailCutProcess;
    }

    public void setListPurchaseDetailCutProcess(List<PurchaseDetailCutProcess> listPurchaseDetailCutProcess) {
        this.listPurchaseDetailCutProcess = listPurchaseDetailCutProcess;
    }

    @Column(name = "current_quantity")
    public Double getCurrentQuantity() {
        return currentQuantity;
    }

    public void setCurrentQuantity(Double currentQuantity) {
        this.currentQuantity = currentQuantity;
    }

    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "purchaseDetail")
    public List<ProcessDressmakerPurchaseDetail> getListProcessDressmakerPurchaseDetail() {
        return listProcessDressmakerPurchaseDetail;
    }

    public void setListProcessDressmakerPurchaseDetail(List<ProcessDressmakerPurchaseDetail> listProcessDressmakerPurchaseDetail) {
        this.listProcessDressmakerPurchaseDetail = listProcessDressmakerPurchaseDetail;
    }
    
    @Transient
    public Double getPackagesTotal(){
        Double total=0.0D;
        for (PackageDetail pd:getPackageDetails()){
            total+=pd.getQuantity();
        }
        return total;
    }

}
